Skip to content

Add pg-status: PostgreSQL instance monitoring utility - #26

Open
gpas45 wants to merge 2 commits into
mainfrom
claude/pgsql-status-utility-ulxf4v
Open

Add pg-status: PostgreSQL instance monitoring utility#26
gpas45 wants to merge 2 commits into
mainfrom
claude/pgsql-status-utility-ulxf4v

Conversation

@gpas45

@gpas45 gpas45 commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Summary

Add pg-status, a read-only Go utility for monitoring PostgreSQL and Postgres Pro instances on a host. The tool discovers instances via systemd, reports their status, and collects detailed statistics from running servers.

Key Changes

  • New utility: pgsql/pg-status/main.go — complete implementation with ~574 lines

    • Instance discovery via systemd units (supports both Postgres Pro 1C and standard PostgreSQL naming schemes)
    • Status reporting: systemd state, port, data directory, disk usage
    • Server statistics collection via psql: version, uptime, connections, database sizes, cache hit ratio, transaction counts
    • Color-coded output with automatic terminal detection and NO_COLOR support
    • Direct port querying mode (-p flag) for non-systemd scenarios
    • Privilege handling: works from root (via runuser/sudo) or postgres user for peer authentication
  • Documentation: pgsql/pg-status/README.md — usage guide with examples and dependencies

  • Tests: pgsql/pg-status/main_test.go — unit tests for utility functions (human, duration, engineLabel, unitBin)

  • Module file: pgsql/pg-status/go.mod — Go module definition (no external dependencies, uses only stdlib)

  • Index update: pgsql/README.md — added reference to new utility

Notable Implementation Details

  • Instance discovery: Combines multiple sources (systemd list-units, .wants symlinks, drop-in directories) with deduplication
  • Port detection: Multi-fallback strategy (active socket via ss, postgresql.conf, PGPORT environment variable)
  • Data directory discovery: Reads from process cmdline, systemd Environment, and EnvironmentFiles
  • Formatting utilities: Human-readable byte sizes (B/KiB/MiB/GiB/TiB/PiB) and duration formatting (days/hours/minutes)
  • SQL queries: Efficient single-query approach for scalar statistics and separate query for database listing
  • Privilege model: Automatic detection of execution context with appropriate command prefixing for privilege escalation

https://claude.ai/code/session_01KVXrszT51udmuntdfAHMVo

claude added 2 commits July 12, 2026 12:32
…state

Read-only tool that discovers PostgreSQL and Postgres Pro 1C instances on
the host (both the postgrespro-<ver>[@PORT] scheme and Debian's
postgresql@<ver>-<cluster>) and prints, per instance: running/stopped
status, port, data directory and its on-disk size, plus — when running —
version, uptime, connections, total database weight, cache hit ratio,
commit/rollback and a size-sorted list of databases. Standard library
only; connects via psql peer-auth as postgres.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KVXrszT51udmuntdfAHMVo
…o-diag.sh

The Lint workflow was failing on main (SC2318, SC2034, SC2010), which
blocked CI on this branch too. Split multi-variable local declarations so
dependent assignments take effect, drop the unused tmpl capture, and
replace `ls | grep` with a glob loop. No behaviour change.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KVXrszT51udmuntdfAHMVo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants